/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.helper.EncCommonHelper;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class BusinessBillListForTaskExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"BusinessBillListForTask.xls\"");
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle boldStyle = workbook.createCellStyle();
		boldStyle.setFont(boldFont);
		boldStyle.setBorderBottom(1);
		boldStyle.setBorderTop(1);
		boldStyle.setBorderLeft(1);
		boldStyle.setBorderRight(1);
		HSSFCellStyle norStyle = workbook.createCellStyle();
		norStyle.setBorderBottom(1);
		norStyle.setBorderTop(1);
		norStyle.setBorderLeft(1);
		norStyle.setBorderRight(1);
		HSSFSheet sheet = workbook.createSheet("list");
		List list = (List) model.get("list");
		List mtList = (List) model.get("mtList");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		byte colNum = 12;

		int sf;
		for (sf = 1; sf < colNum; ++sf) {
			r.createCell((short) sf);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString("生产进度表"));
		sheet.addMergedRegion(new Region(0, 0, 0, (short) (colNum - 1)));
		r = sheet.createRow(1);
		c = r.createCell(0);

		for (sf = 1; sf < colNum; ++sf) {
			r.createCell((short) sf);
		}

		c.setCellValue(new HSSFRichTextString("任务数量：" + list.size()));
		sheet.addMergedRegion(new Region(1, 0, 1, (short) (colNum - 1)));
		r = sheet.createRow(2);
		c = r.createCell(0);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("日期"));
		c = r.createCell(1);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(2);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("订货单位"));
		c = r.createCell(3);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("项目类型"));
		c = r.createCell(4);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("内容"));
		c = r.createCell(5);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("材质"));
		c = r.createCell(6);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("规格"));
		c = r.createCell(7);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(8);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(9);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("制作要求"));
		c = r.createCell(10);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("当前工序"));
		c = r.createCell(11);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("完成情况"));
		SimpleDateFormat arg24 = new SimpleDateFormat("yyyy-MM-dd");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			String spe = (String) objs[9];
			String priceType = (String) objs[13];
			String lengType = (String) objs[22];
			if (priceType.equals("AREA") || priceType.equals("LENGTH")) {
				spe = spe
						+ " x "
						+ EncCommonHelper
								.parseBigDecimal((BigDecimal) objs[11]);
			}

			BigDecimal amount;
			if (!priceType.equals("AREA") && !priceType.equals("LENGTH")) {
				amount = (BigDecimal) objs[11];
			} else {
				amount = (BigDecimal) objs[12];
			}

			String unit = "";
			if (priceType.equals("AREA")) {
				unit = "平方米";
			} else if (priceType.equals("LENGTH")) {
				if (lengType.equals("M")) {
					unit = "米";
				} else {
					unit = "公分";
				}
			} else if (priceType.equals("AMOUNT")) {
				unit = (String) objs[10];
			}

			String estName = "";
			if (!EncCommonHelper.checkPurePrivilege(request, "NO_CLIE_NAME")) {
				estName = (String) objs[4];
			}

			r = sheet.createRow(os + 3);
			c = r.createCell(0);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg24.format((Date) objs[1])));
			c = r.createCell(1);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString((String) objs[2]));
			c = r.createCell(2);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(estName));
			c = r.createCell(3);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(mtList,
							Long.parseLong(objs[6].toString()))));
			c = r.createCell(4);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString((String) objs[7]));
			c = r.createCell(5);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString((String) objs[8]));
			c = r.createCell(6);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(spe));
			c = r.createCell(7);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(unit));
			c = r.createCell(8);
			c.setCellStyle(norStyle);
			c.setCellValue(amount.doubleValue());
			c = r.createCell(9);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString((String) objs[14]));
			c = r.createCell(10);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString((String) objs[19]));
			c = r.createCell(11);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString((String) objs[15]));
		}

		ServletOutputStream arg25 = response.getOutputStream();
		workbook.write(arg25);
		arg25.flush();
		arg25.close();
	}
}